package com.babyqiao.wallpaper.manager.service.impl;

import com.babyqiao.wallpaper.manager.dao.AlbumPicMapper;
import com.babyqiao.wallpaper.manager.entity.WallpaperAlbumPic;
import com.babyqiao.wallpaper.manager.controller.dto.AlbumPicQuery;
import com.babyqiao.wallpaper.manager.entity.vo.AlbumPicResVO;
import com.babyqiao.wallpaper.manager.service.AlbumPicService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

/**
 * @author qinglongzhai
 * @date 2020/12/10
 */
@Service
public class AlbumPicServiceImpl extends ServiceImpl<AlbumPicMapper, WallpaperAlbumPic> implements AlbumPicService {
    @Autowired
    private AlbumPicMapper albumPicMapper;

    @Override
    /**
     *构建查询专辑图片的条件
     * @Author qinglongZhai
     * @Date 2020/12/10
     * * @param albumPicQuery:
     * * @return: com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<com.babyqiao.wallpaper.manager.entity.AlbumPic>
     */

    public QueryWrapper<WallpaperAlbumPic> addSelectCondition(AlbumPicQuery albumPicQuery) {
        QueryWrapper<WallpaperAlbumPic> queryWrapper = new QueryWrapper<>();
        String albumId = albumPicQuery.getAlbumId();
        String name = albumPicQuery.getName();
        String online = albumPicQuery.getOnline();

        String startTime = albumPicQuery.getStartTime();
        String endTime = albumPicQuery.getEndTime();

        if (albumId != null) {
            queryWrapper.eq("album_id", albumId);
        }
        if (name != null) {
            queryWrapper.like("name", name);
        }
        if (online != null) {
            queryWrapper.eq("online", online);
        }
        if (startTime != null) {
            queryWrapper.ge("gmt_modified", startTime);
        }
        if (endTime != null) {
            queryWrapper.le("gmt_modified", endTime);
        }

        //排序
        queryWrapper.orderByDesc("gmt_modified");
        return queryWrapper;


    }

    /**
     * 多表带条件带分页查询
     *
     * @param page
     * @param albumPicQuery :
     * @Author qinglongZhai
     * @Date 2020/12/10
     * * @param page:
     */
    @Override
    public Page<AlbumPicResVO> multipleTablesAndCondition(Page<AlbumPicResVO> page, AlbumPicQuery albumPicQuery) {
        Page<AlbumPicResVO> andPage = albumPicMapper.findAndPage(page, albumPicQuery);

        return andPage;
    }

    /**
     * @param id    图片ID
     * @param state : 要修改的上下状态
     * @Author qinglongZhai
     * @Date 2020/12/11
     * * @param id:
     */
    @Caching(evict = {
            @CacheEvict(value = "AlbumPicContent",allEntries=true)
    })
    @Override
    public UpdateWrapper<WallpaperAlbumPic> addUpdataCondition(String id, Integer state) {
        UpdateWrapper<WallpaperAlbumPic> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        updateWrapper.set("online", state);
        return updateWrapper;
    }
}

